package com.nowcoder.community.controller;/**
 * @description:
 * @Author HP
 * @create 2022/1/3 19:39
 */

import com.nowcoder.community.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;

/**
 * @author HP
 * @create 2022-01-03 19:39
 * @desc
 **/
@Controller
public class DataController {

    @Autowired
    private DataService dataService;

    // 访问统计页面
    @RequestMapping(path = "/data",method = {RequestMethod.GET,RequestMethod.POST}) //支持POST请求才能被转发
    public String getDataPage(){
        return "/site/admin/data";
    }

    //统计网站UV
    @RequestMapping(path = "/data/uv",method = RequestMethod.POST)
    public String getUV(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date end, Model model){
        long uv = dataService.calculateUV(start,end);
        model.addAttribute("uvResult",uv);
        model.addAttribute("uvStartDate",start);
        model.addAttribute("uvEndDate",end);
        return "forward:/data";    //转发(处理一半，然后交给与它平级的/data接着处理（即复用了getDataPage方法，但还是同一个请求，所以要求/data页支持post），而不是重定向到模板)
    }

    //统计活跃用户
    @RequestMapping(path = "/data/dau",method = RequestMethod.POST)
    public String getDAU(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
                         @DateTimeFormat(pattern = "yyyy-MM-dd") Date end, Model model){
        long dau = dataService.calculateDAU(start,end);
        model.addAttribute("dauResult",dau);
        model.addAttribute("dauStartDate",start);
        model.addAttribute("dauEndDate",end);
        return "forward:/data";    //转发(处理一半，然后交给与它平级的/data接着处理
    }
}
